﻿using System;
using System.ComponentModel;
using System.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class FrmNotify
    {
        public int Time { get; set; } = 5;

        public string ContentStr { get; set; }

        public int ParentTop { get; set; }

        public NotifyType NotifyType { get; set; }

        /// <summary>
        /// 通知关闭后关闭当前窗体
        /// </summary>
        public bool? CloseOwner { get; set; }

        private DispatcherTimer _timer;

        public FrmNotify()
        {
            InitializeComponent();
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += _timer_Tick;
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke(delegate
            {
                Time--;
                TxtTime.Text = Time + "";
                if(Time<=0) Close();
               
            });
        }

        private void Animation_Completed(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmNotify_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void FrmNotify_OnLoaded(object sender, RoutedEventArgs e)
        {
            TxtContent.Text = ContentStr;
            TxtTime.Text = Time + "";
            var fd = new SolidColorBrush(Color.FromRgb(0xd6, 0xd6, 0xd6));
            switch (NotifyType)
            {
                case NotifyType.Success:
                    fd = new SolidColorBrush(Color.FromRgb(0x22, 0xac, 0x38));
                    break;

                case NotifyType.Alarm:
                    fd = new SolidColorBrush(Color.FromRgb(0xfc, 0xc7, 0x00));
                    break;

                case NotifyType.Error:
                    fd = new SolidColorBrush(Colors.Red);
                    break;
                default:
                    break;

            }

            TxtContent.Foreground = fd;
            TxtTime.Foreground = fd;
            Border.BorderBrush = fd;

            UpdateLayout();
            SystemSounds.Asterisk.Play();//播放提示声

            var animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));//NotifyTimeSpan是自己定义的一个int型变量，用来设置动画的持续时间
            animation.From = 0;
            animation.To = 40;
            BeginAnimation(HeightProperty, animation);//设定动画应用于窗体的Left属性

            if (Time <= 0)
            {
                _timer.IsEnabled = false;
                TxtTime.Visibility = Visibility.Collapsed;
                Button.Visibility = Visibility.Visible;
            }
            else
            {
                _timer.IsEnabled = true;
                TxtTime.Visibility = Visibility.Visible;
                Button.Visibility = Visibility.Collapsed;
            }
        }

        private void FrmNotify_OnClosing(object sender, CancelEventArgs e)
        {
            _timer.IsEnabled = false;
            _timer.Tick -= _timer_Tick;
            _timer = null;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
